﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CheckNameProject
{
    public partial class Report : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
            }
        }

        protected void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbType.SelectedValue == 1.ToString())
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
            }
            else
            {
                GridView1.Visible = false;
                GridView2.Visible = true;
                GridView2.DataSource = SqlDataSource2;
                GridView2.DataBind();
            }

        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            HiddenField HD = (HiddenField)(GridView1.Rows[e.NewSelectedIndex].FindControl("HiddenField1"));
            //string url = "ReportDoc.aspx?DocID=" + HD.Value;
            Response.Redirect("ReportDoc.aspx?DocID=" + HD.Value);
        }

        protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            HiddenField HD = (HiddenField)(GridView2.Rows[e.NewSelectedIndex].FindControl("HiddenField2"));
            Response.Redirect("ReportPer.aspx?PersonID=" + HD.Value);
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            GridView2.DataSource = SqlDataSource2;
            GridView2.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DBNameCheckDataContext db = new DBNameCheckDataContext();
            if (cbType.SelectedValue == 1.ToString())
            {
                if (!txtSearch.Text.Equals(""))
                {

                    var q = from p in db.Documents
                            where p.DocumentName.Contains(txtSearch.Text) && p.StatusID == 1
                            orderby p.Date descending
                            select p;

                    GridView1.DataSource = q.ToList();
                    GridView1.DataBind();

                    txtSearch.Text = "";
                }
                else
                {
                    GridView1.DataSource = SqlDataSource1;
                    GridView1.DataBind();
                }
            }
            else
            {
                if (!txtSearch.Text.Equals(""))
                {
                    string[] Name = new string[2];
                    Name = txtSearch.Text.Split(' ');
                    if (Name.Length != 1)
                    {
                        var q = from p in db.Persons
                                where p.FirstName.Contains(Name[0]) && p.LastName.Contains(Name[1])
                                select new
                                {
                                    ชื่อและนามสกุล = p.FirstName + " " + p.LastName,
                                    PersonID = p.PersonID
                                };
                        GridView2.DataSource = q.ToList();
                        GridView2.DataBind();

                        txtSearch.Text = "";
                    }
                    else
                    {
                        var q = from p in db.Persons
                                where p.FirstName.Contains(Name[0]) || p.LastName.Contains(Name[0])
                                select new
                                {
                                    ชื่อและนามสกุล = p.FirstName + " " + p.LastName,
                                    PersonID = p.PersonID
                                };
                        GridView2.DataSource = q.ToList();
                        GridView2.DataBind();

                        txtSearch.Text = "";
                    }
                    
                }
                else
                {
                    GridView2.DataSource = SqlDataSource2;
                    GridView2.DataBind();
                }
            }
            
        }
    }
}